package com.elm.controller;

import com.elm.common.ApiResponse;
import com.elm.po.Business;
import com.elm.po.Food;
import com.elm.service.FoodService;

import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/api")
public class FoodController {
	
    @Autowired
    private FoodService foodService;

    @GetMapping("/foods/{id}")
    public ApiResponse<Food> getFoodById(@PathVariable int id) {
    	try {
    		Food food = foodService.getFoodById(id);
    		if(food==null) {
    			return ApiResponse.error("404", "找不到对应商品");
    		}
            return ApiResponse.success(food);
        } catch (RuntimeException e) {
            return ApiResponse.error("400", e.getMessage());
        } catch (Exception e) {
            return ApiResponse.error("500", "系统错误：" + e.getMessage());
        }
    }
    
    @GetMapping("/foods/{businessId}/businessId")
    public ApiResponse<List<Food>> listAvailableFoodByBusinessId(@PathVariable int businessId) {
    	try {
            return ApiResponse.success(foodService.getFoodByBusinessId(businessId));
        } catch (RuntimeException e) {
            return ApiResponse.error("400", e.getMessage());
        } catch (Exception e) {
            return ApiResponse.error("500", "系统错误：" + e.getMessage());
        }
    }

    @GetMapping("/foods/reviewing")
    public ApiResponse<List<Food>> getAllReviewingFood() {
    	try {
            return ApiResponse.success(foodService.getAllReviewingFood());
        } catch (RuntimeException e) {
            return ApiResponse.error("400", e.getMessage());
        } catch (Exception e) {
            return ApiResponse.error("500", "系统错误：" + e.getMessage());
        }
    }
    
    @PatchMapping("/foods/{id}")
    public ApiResponse<Food> patchFood(@PathVariable int id,@RequestBody Food food) {
    	try {
    		food.setId(id);
    		Food resultfood = foodService.updateFoodInfo(food);
    		if(resultfood==null) {
    			return ApiResponse.error("404", "找不到对应商品");
    		}
            return ApiResponse.success(resultfood);
        } catch (RuntimeException e) {
            return ApiResponse.error("400", e.getMessage());
        } catch (Exception e) {
            return ApiResponse.error("500", "系统错误：" + e.getMessage());
        }
    }
    
    @GetMapping("/foods")
    public ApiResponse<List<Food>> getAllFoods(@RequestParam (required = false) Integer business,@RequestParam (required = false) Integer order) {
    	try {
    		if(business!=null)
    			return ApiResponse.success(foodService.getFoodByBusinessId(business));
    		else
    			return ApiResponse.success(foodService.getAllFoodByOrder(order));
        } catch (RuntimeException e) {
            return ApiResponse.error("400", e.getMessage());
        } catch (Exception e) {

            return ApiResponse.error("500", "系统错误：" + e.getMessage());
        }
    }
    

    @PutMapping("/foods")
    public ApiResponse<Food> updateFoodInfo(@RequestBody Food food) {
    	try {
    		Food updatedfood = foodService.updateFoodInfo(food);
    		if(updatedfood==null) {
    			return ApiResponse.error("500", "更新商品失败");
    		}
            return ApiResponse.success(updatedfood);
        } catch (RuntimeException e) {
            return ApiResponse.error("400", e.getMessage());
        } catch (Exception e) {
            return ApiResponse.error("500", "系统错误：" + e.getMessage());
        }
    }

    @PostMapping("/foods")
    public ApiResponse<Food> createFood(@RequestBody Food food) {
    	try {
            int result = foodService.createFood(food);
            if (result <= 0) {
                return ApiResponse.error("500", "商家创建失败");
            }
            Food createdFood = foodService.getFoodByNameByBusiness(food.getFoodName(),food.getBusiness().getId());
            return ApiResponse.success(createdFood);
        } catch (RuntimeException e) {
            return ApiResponse.error("400", e.getMessage());
        } catch (Exception e) {
            return ApiResponse.error("500", "系统错误：" + e.getMessage());
        }
    }
    
    @PutMapping("/foods/status/{foodId}/{status}")
    public ApiResponse<Food> updateFoodStatus(@PathVariable Integer foodId,@PathVariable String status) {
    	try {
    		Food updatedfood = foodService.updateFoodStatus(foodId,status);
    		if(updatedfood==null) {
    			return ApiResponse.error("500", "更新商品状态失败");
    		}
            return ApiResponse.success(updatedfood);
        } catch (RuntimeException e) {
            return ApiResponse.error("400", e.getMessage());
        } catch (Exception e) {
            return ApiResponse.error("500", "系统错误：" + e.getMessage());
        }
    }
}



